import { Bump, Tink, keyboard, hitTestRectangle } from './utils.js';
import * as PIXI from 'pixi.js';

let gameInfo = {
  size: 48,
  width: 25,
  height: 15,
  resolution: 2
};
const gameApp = new PIXI.Application({
  width: gameInfo.size * gameInfo.width,
  height: gameInfo.size * gameInfo.height,
  antialias: true, // default: false 反锯齿
  transparent: false, // default: false 透明度
  resolution: gameInfo.resolution // default: 1 分辨率
});
var bump = new Bump(PIXI);
let tink = new Tink(PIXI, gameApp.view);
// gameApp.renderer.backgroundColor = 0xff0000;
gameApp.view.className = 'canvas-map';
// gameApp.view.style.width = gameApp.screen.width + 'px';
// gameApp.view.style.height = gameApp.screen.height + 'px';
function autoResize () {
  var scale = 0.9;
  if (innerWidth > innerHeight) {
    gameApp.view.style.width = innerHeight * (gameApp.screen.width / gameApp.screen.height) * scale + 'px';
    // 5:3
    gameApp.view.style.height = innerHeight * scale + 'px';
  } else {
    scale = 1;
    gameApp.view.style.width = innerWidth * scale + 'px';
    // 5:3
    gameApp.view.style.height = innerWidth / (gameApp.screen.width / gameApp.screen.height) * scale + 'px';
  }
}
autoResize();
window.addEventListener('resize', () => {
  autoResize();
});
document.body.appendChild(gameApp.view);
var mainStage = new PIXI.Container();
// 粒子容器 高性能
var particleStage = new PIXI.ParticleContainer(1000, {
  position: true,
  rotation: false,
  alphaAndtint: false,
  scale: false,
  uvs: false
});
var mainLoader = new PIXI.Loader();
gameApp.stage.addChild(particleStage);
gameApp.stage.addChild(mainStage);
gameApp.stage.zIndex = 2;
// 精灵集合
// 硬块合集
var arrSpriteWall = [];
// 炸弹合集{timeout,sprite}
var arrBomb = [];
// 爆炸范围
var arrBlast = [];
// 检测碰撞
// 检测指定精灵是否和硬块碰撞
function isHitWall (sprite) {
  var isHit = arrSpriteWall.some(item => {
    return bump.hit(sprite, item, true, true, true, function (direction, arrsprite) {
      // console.log(direction);
      // console.log('撞墙了');
    });
  });
  return isHit;
}
function isHitBomb (sprite) {
  var currentItem;
  var isHit = arrBomb.some(item => {
    // 两圆相切 , 两圆心距离等于 两圆圈半径之和
    var dis = Math.abs(Math.pow(sprite.x - item.x, 2) + Math.pow(sprite.y - item.y, 2));
    if (Math.sqrt(dis) > item.width / 2 + sprite.width / 2) {
      // console.log(Math.sqrt(dis), item.width / 2 + sprite.width / 2);
      item.selfUseHit = true;
    }
    currentItem = item;
    return bump.hitTestRectangle(sprite, item) && item.selfUseHit;
  });
  return { isHit, item: currentItem };
}
let keyObjectW = keyboard('w');
let keyObjectA = keyboard('a');
let keyObjectS = keyboard('s');
let keyObjectD = keyboard('d');
let keyObjectSpace = keyboard(' ');
// 获得移动方向
function getArrow () {
  var keys = [keyObjectW, keyObjectA, keyObjectS, keyObjectD];
  for (let item of keys) {
    if (item.isDown) {
      return item.value;
    }
  }
  return '';
}
mainLoader.add('main', './img/main.json').add('./img/bomp.json').load((loader, res) => {
  const mainTextures = res['main'].textures;
  const bompTextures = res['./img/bomp.json'].textures;
  // 25*14
  for (let i = 0; i < 25; i++) {
    for (let j = 0; j < 15; j++) {
      let caopi = new PIXI.Sprite(mainTextures['caopi.png']);
      caopi.position.set(i * 48, j * 48);
      caopi.width = 48;
      caopi.height = 48;
      particleStage.addChild(caopi);
    }
  }
  for (let i = 1; i < 24; i += 2) {
    for (let j = 1; j < 14; j += 2) {
      let wall = new PIXI.Sprite(mainTextures['wall.png']);
      wall.position.set(i * 48, j * 48);
      wall.width = 48;
      wall.height = 48;
      arrSpriteWall.push(wall);
    }
  }
  particleStage.addChild(...arrSpriteWall);
  (function () {
    // new PIXI.Loader().add('hj2', './img/css_sprites.png').load((loader, resources) => {
    //   setTimeout(() => {
    //     var hjaText2 = PIXI.utils.TextureCache['hj2'];
    //     let rectangle2 = new PIXI.Rectangle(98, 12, 60, 60);
    //     hjaText2.frame = rectangle2;
    //     // console.log((hjaText2.frame = [new PIXI.Rectangle(98, 12, 60, 60), new PIXI.Rectangle(98, 12, 60, 60)]));
    //     let hja = new PIXI.Sprite(hjaText2);
    //     console.log(hjaText2);
    //     gameApp.stage.addChild(hja);
    //   }, 1000);
    // });
    var loader3 = new PIXI.Loader();
    loader3.add('tieba', require('../img/tieba.json')).load((loader, res) => {
      const tiebaText = res['tieba'].textures;
      // let hja = new PIXI.Sprite(tiebaText['huaji.png']);
      var arrTextQiqiu = [
        PIXI.Texture.from('qiqiu1.png'),
        PIXI.Texture.from('qiqiu2.png'),
        PIXI.Texture.from('qiqiu3.png'),
        PIXI.Texture.from('qiqiu2.png')
      ];
      let hja = new PIXI.AnimatedSprite(arrTextQiqiu);
      hja.animationSpeed = 0.06;
      hja.play();
      // let yinxian = new PIXI.Sprite(tiebaText['yinxian.png']);
      hja.width = 36;
      hja.height = 36;
      hja.zIndex = 3;
      hja.position.set(0, 14 * 48);
      // hja.scale.set(0.5, 0.5);
      // gameApp.stage.addChild(yinxian);
      mainStage.addChild(hja);

      hja.vx = 0;
      hja.vy = 0;
      const speed = 1 * 3;
      gameApp.ticker.add(delta => gameLoop(delta));
      function gameLoop (delta) {
        //Move the cat 1 pixel
        // console.log(hja.x, hja.y);
        // console.log(gameApp.screen.width, gameApp.screen.height);
        // 控制速度应这样放在检测范围前, 否则会抖动(检测后 才移动 ,一秒检测六十次, 下次检测才会修正)
        isHitWall(hja);
        hja.x += hja.vx;
        hja.y += hja.vy;
        var oHitInfo = isHitBomb(hja);
        if (oHitInfo.isHit) {
          // oHitInfo.item;
          hja.vy = 0;
          hja.vx = 0;
        }

        if (hja.x > gameApp.screen.width - hja.width) {
          hja.x = gameApp.screen.width - hja.width;
        } else if (hja.x < 0) {
          hja.x = 0;
        }

        if (hja.y > gameApp.screen.height - hja.height) {
          hja.y = gameApp.screen.height - hja.height;
        } else if (hja.y < 0) {
          hja.y = 0;
        } else {
        }

        // console.log(isHitWall(hja));
        // console.log(getArrow());
        // if (isHitWall(hja)) {
        // var arrow = getArrow();
        // console.log(isHitWall(hja));
        // hja.x += hja.vx;
        // hja.y += hja.vy;
      }
      keyObjectSpace.press = () => {
        if (
          !arrBomb.some(item => {
            return item.x == hja.x && item.y == hja.y;
          })
        ) {
          var arrTextBomp = [
            PIXI.Texture.from('bomp1.png'),
            PIXI.Texture.from('bomp2.png'),
            PIXI.Texture.from('bomp3.png'),
            PIXI.Texture.from('bomp3.png'),
            PIXI.Texture.from('bomp2.png'),
            PIXI.Texture.from('bomp2.png'),
            PIXI.Texture.from('bomp1.png')
          ];
          // 或者从命名纹理取,  PIXI.Texture.from无视bompTextures命名, 可以直接命中具体纹理名
          //  var arrTextBomp = [bompTextures['bomp1.png'], bompTextures['bomp2.png'], bompTextures['bomp3.png']];
          let bombSprite = new PIXI.AnimatedSprite(arrTextBomp, true);
          // let bombSprite = new PIXI.Sprite(tiebaText['yinxian.png']);
          bombSprite.x = hja.x;
          bombSprite.y = hja.y;
          bombSprite.scale = 2;
          bombSprite.width = 36;
          bombSprite.height = 36;
          // bombSprite.selfUseHit = false;
          bombSprite.animationSpeed = 0.1;
          bombSprite.loop = true;
          bombSprite.play();
          console.log(bombSprite);
          gameApp.stage.addChild(bombSprite);
          arrBomb.push(bombSprite);
          function setSize (s) {
            s.width = 48;
            s.height = 48;
          }
          setTimeout(() => {
            /* 威力 4 */
            for (let i = 0; i < 3; i++) {
              let flameSpriteXR = new PIXI.Sprite(bompTextures['flame.png']);
              let flameSpriteXL = new PIXI.Sprite(bompTextures['flame.png']);
              let flameSpriteYT = new PIXI.Sprite(bompTextures['flame.png']);
              let flameSpriteYB = new PIXI.Sprite(bompTextures['flame.png']);
              flameSpriteXR.width = flameSpriteXR.width = flameSpriteXR.height = 48;
              if (i === 0) {
                let flameSpriteorigin = new PIXI.Sprite(bompTextures['flameCenter.png']);
                flameSpriteorigin.position.set(bombSprite.x, bombSprite.y);
                flameSpriteorigin.zIndex = -1;
                setSize(flameSpriteorigin);
                gameApp.stage.addChild(flameSpriteorigin);
              } else {
                flameSpriteXR.position.set(bombSprite.x + (i + 1) * gameInfo.size, bombSprite.y);
                flameSpriteXL.position.set(bombSprite.x - (i + 1) * gameInfo.size, bombSprite.y);
                flameSpriteYB.position.set(bombSprite.x, bombSprite.y + (i + 1) * gameInfo.size);
                flameSpriteYT.position.set(bombSprite.x, bombSprite.y - (i + 1) * gameInfo.size);
                setSize(flameSpriteXR);
                setSize(flameSpriteXL);
                setSize(flameSpriteYB);
                setSize(flameSpriteYT);
                gameApp.stage.addChild(flameSpriteXR, flameSpriteXL, flameSpriteYT, flameSpriteYB);
              }
            }
            // 3s后引爆 、在其他炸弹爆炸范围内立即引爆
            gameApp.stage.removeChild(bombSprite);
            arrBomb.splice(arrBomb.indexOf(arrBomb), 1);
          }, 3000);
        }
      };
      keyObjectW.press = () => {
        // 将每个草皮存入数组 或者 用mainState舞台childern来遍历
        // hitTestRectangle(hja, caopi);
        hja.vy = -speed;
      };
      keyObjectW.release = () => {
        hja.vy = 0;
      };
      keyObjectA.press = () => {
        hja.vx = -speed;
      };
      keyObjectA.release = () => {
        hja.vx = 0;
      };
      keyObjectS.press = () => {
        hja.vy = speed;
      };
      keyObjectS.release = () => {
        hja.vy = 0;
      };
      keyObjectD.press = () => {
        hja.vx = speed;
        console.log(hja.x);
      };
      keyObjectD.release = () => {
        hja.vx = 0;
      };
    });
  })();
});
gameApp.render(mainStage);
var ccc = 1;
var ddd = 2;
export { ccc, ddd };
export default gameApp;
